package com.dunglv.flashlight;

import android.hardware.Camera;

public class StrobeRunner implements Runnable {

	protected StrobeRunner() {

	}

	public static StrobeRunner getInstance() {
		return (instance == null ? instance = new StrobeRunner() : instance);
	}

	private static StrobeRunner instance;

	public volatile boolean requestStop = false;
	public volatile boolean isRunning = false;
	public volatile int delay = 1;
	// public volatile int delayoff = 500;
	public volatile MainActivity controller;
	public volatile String errorMessage = "";
	int constantDelay = 20;

	@Override
	public void run() {
		if (isRunning)
			return;

		requestStop = false;
		isRunning = true;

		Camera cam = Camera.open();

		Camera.Parameters pon = cam.getParameters(), poff = cam.getParameters();

		pon.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
		poff.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);

		while (!requestStop) {
			try {
				if (delay != 0) {
					cam.setParameters(pon);
					Thread.sleep(delay * constantDelay);
					cam.setParameters(poff);
					Thread.sleep(delay * constantDelay);
				} else {
					cam.setParameters(pon);
					cam.startPreview();
				}
			} catch (InterruptedException ex) {

			} catch (RuntimeException ex) {
				requestStop = true;
				errorMessage = "Error setting camera flash status. Your device may be unsupported.";
			}
		}

		cam.release();

		isRunning = false;
		requestStop = false;
	}

}
